package Chapter04.TS;

import java.util.HashMap;
import java.util.Map;

public class DVDStore {
    private Map<String, Integer> dvdMap;

    public DVDStore() {
        dvdMap = new HashMap<String, Integer>();
    }

    public void addDVD(String title, int copies) {
        dvdMap.put(title, copies);
    }

    public void removeDVD(String title) {
        dvdMap.remove(title);
    }

    public void rentDVD(String title) {
        if (dvdMap.containsKey(title)) {
            int copies = dvdMap.get(title);
            if (copies > 0) {
                dvdMap.put(title, copies - 1);
                System.out.println("The DVD " + title + " has been rented.");
            } else {
                System.out.println("Sorry, the DVD " + title + " is out of stock.");
            }
        } else {
            System.out.println("Sorry, the DVD " + title + " is not available.");
        }
    }

    public void returnDVD(String title) {
        if (dvdMap.containsKey(title)) {
            int copies = dvdMap.get(title);
            dvdMap.put(title, copies + 1);
            System.out.println("The DVD " + title + " has been returned.");
        } else {
            dvdMap.put(title, 1);
            System.out.println("A new DVD " + title + " has been added to the store.");
        }
    }

    public void displayStock() {
        System.out.println("DVD Stock:");
        for (Map.Entry<String, Integer> entry : dvdMap.entrySet()) {
            System.out.println(entry.getKey() + " - " + entry.getValue() + " copies");
        }
    }

    public static void main(String[] args) {
        DVDStore store = new DVDStore();
        store.addDVD("The Shawshank Redemption", 5);
        store.addDVD("The Godfather", 3);
        store.addDVD("The Dark Knight", 2);
        store.displayStock();
        store.rentDVD("The Shawshank Redemption");
        store.rentDVD("The Shawshank Redemption");
        store.rentDVD("The Godfather");
        store.returnDVD("The Shawshank Redemption");
        store.displayStock();
    }
}
